﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SocialLearning
{
    public partial class GroupUpdateForm : Form
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="GroupUpdateForm"/> class.
        /// </summary>
        public GroupUpdateForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Loads the CBX data.
        /// </summary>
        private void LoadCbxData()
        {
            List<string> groupList = UAC_GroupManagement.Instance.SeeGroup().ToList();
            cbxUpdateGroup.Items.Clear();
            foreach (string groupname in groupList)
            {
                cbxUpdateGroup.Items.Add(groupname);
            }
        }

        /// <summary>
        /// Updates the group.
        /// </summary>
        /// <param name="oldName">The old name.</param>
        /// <param name="newName">The new name.</param>
        private void UpdateGroup(string oldName, string newName)
        {
            try
            {
                UAC_GroupManagement.Instance.UpdateGroup(oldName, newName);
                txtUpdateName.Clear();
                cbxUpdateGroup.Text = "";
                LoadCbxData();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region Private Controls

        /// <summary>
        /// Handles the Click event of the btnUpdate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtUpdateName.Text) && string.IsNullOrWhiteSpace(cbxUpdateGroup.Text))
            {
                MessageBox.Show("Please enter a group name!", "Information",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                UpdateGroup(cbxUpdateGroup.Text, txtUpdateName.Text);
                MessageBox.Show(string.Format("{0} group was updated to {1} group.", cbxUpdateGroup.Text, txtUpdateName.Text),
                            "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// Close the form
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Load data into combobox
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void GroupUpdateForm_Load(object sender, EventArgs e)
        {
            LoadCbxData();
        }

        #endregion
    }
}
